package jhelp;

import java.util.ArrayList;
import java.util.Hashtable;

public abstract class DataModel implements Observable, Model {
	private String dataSourcePath;
	private DataProcessor dataProcessor;
	private Record currentRecord;
	private Hashtable<String, Record> dataTable;
	private ArrayList<Observer> observers = new ArrayList<Observer>();
	
	public Record getRecord() {
		return currentRecord;
	}
	
	public DataModel(){
		dataProcessor = createDataProcessor();
	}

	public void setRecord(Record Record) {
		this.currentRecord = Record;
	}

	public String getDataSourcePath() {
		return dataSourcePath;
	}

	public void setDataSourcePath(String dataSourcePath) {
		this.dataSourcePath = dataSourcePath;
	}
	
	public void setDataSource(String filePath){
		this.dataSourcePath = filePath;
		dispatchData();
		//notifyObservers();
	}
	
	public abstract DataProcessor createDataProcessor();

	public void addObserver(Object o) {
		observers.add((Observer)o);
	}

	public void removeObderver(Object o) {
		// TODO Auto-generated method stub
	}

	public void notifyObservers() {
		for(Observer o: observers){
			o.update(this);
		}
	}
	private void dispatchData(){
		dataTable = dataProcessor.dispatchDataInHashtable(dataSourcePath);
	}
	public void findText(String strToFind){
		currentRecord = dataTable.get(strToFind);
		if(currentRecord == null){
			currentRecord = new Record(-1, "", "There is no such term in the dictionary");
		}
		notifyObservers();
	}

	public Record getCurrentState() {
		return currentRecord;
	}
}
